//
//  KY21AdvertView.m
//  KYMobClick
//
//  Created by 01 on 16/11/8.
//  Copyright © 2016年 Czj. All rights reserved.
//

#import "KY21AdvertView.h"

@interface KY21AdvertView()
@property (nonatomic, strong) UIImageView                   *imgView;
@property (nonatomic, strong) UIButton                      *closeBtn;
@property (nonatomic, strong) UIImageView                   *closeImageView;
@property (nonatomic, strong) NSMutableDictionary           *dict;
@end

@implementation KY21AdvertView

#pragma mark - Init
/**
 初始化
 */
- (instancetype)init {
    self = [super init];
    if (self) {
        
        //背景View
        UIView *bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        bgView.backgroundColor = [UIColor colorWithRed:33/255 green:33/255 blue:33/255 alpha:0.7];
        CGFloat screenViewX = kRealScreenWidth(20);
        CGFloat screenViewW = kDeviceWidth - kRealScreenWidth(40);
        CGFloat screenViewH = screenViewW/6.*5.;
        CGFloat screenViewY = (kDeviceHeight - screenViewH)/2;
        
        CGFloat imgViewX = kDeviceWidth-kRealScreenWidth(50);
        CGFloat imgViewY = screenViewY - kRealScreenWidth(65);
        
        self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(screenViewX, screenViewY, screenViewW,screenViewH)];
        self.closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.closeBtn.frame = CGRectMake(imgViewX, imgViewY, kRealScreenWidth(30), kRealScreenWidth(30));
        self.closeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(imgViewX, imgViewY, kRealScreenWidth(30), kRealScreenWidth(65))];
        [self addSubview:bgView];
        [bgView addSubview:self.imgView];
        [bgView addSubview:self.closeImageView];
        [bgView addSubview:self.closeBtn];
        self.imgView.layer.cornerRadius = 5.;
        self.imgView.layer.masksToBounds = YES;
        [self createTap];
    }
    return self;
}

#pragma mark - Public methods
/**
 填充视图内容
 */
- (void)createContent:(NSDictionary *)dataDic blockHeight:(void (^)(NSInteger))viewHeight {
    self.dict = [NSMutableDictionary dictionaryWithDictionary:dataDic];
    NSString *pathDocument = [KYSystemInfo getSaveAdvertImagePath];
//    KYLog(@"插屏广告数据：%@",dataDic);
    if (dataDic && dataDic[@"data"]) {
        NSString *imagePath = [pathDocument stringByAppendingString:[NSString stringWithFormat:@"%@.png",self.dict[@"data"][@"adsid"]]];
        UIImage *cacheImage = [UIImage imageWithContentsOfFile:imagePath];
        __weak typeof(self) _self = self;
        [self.imgView KYSD_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:dataDic[@"data"][@"imageurl"]] placeholderImage:cacheImage options:KYSDWebImageAllowInvalidSSLCertificates progress:nil completed:^(UIImage *image, NSError *error, KYSDImageCacheType cacheType, NSURL *imageURL) {
            if (image) {
                __strong typeof(_self) self = _self;
                [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
                CGSize size = image.size;
                CGFloat height= size.height;
                CGFloat width = size.width;
                if (viewHeight) viewHeight(kDeviceWidth / width * height);
                [self postPVData];
//                self.imgView.frame = CGRectMake(0, 0, kDeviceWidth, kDeviceWidth / width * height);
            }else{
            }
        }];
    }
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"kyResource" ofType:@"bundle"]];
    NSString * imagePath = [[bundle resourcePath] stringByAppendingPathComponent:@"screen_close.png"];
    UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
    self.closeImageView.image = img;
    [self.closeBtn addTarget:self action:@selector(remove21View) forControlEvents:UIControlEventTouchUpInside];
}

/**
 创建广告视图点击手势
 */
- (void)createTap {
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeOtherPage:)];
    self.imgView.userInteractionEnabled = YES;
    [self.imgView addGestureRecognizer:tap];
}

/**
 移除视图
 */
- (void)remove21View {
    [self.superview.superview removeFromSuperview];
}

#pragma mark - Response method
/**
 统计PV数据
 */
- (void)postPVData {
    //发送统计数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"advertPVData" object:self.dict[@"data"]];
}
/**
 广告视图事件跳转 同时触发post页面统计请求
 */
- (void) changeOtherPage:(NSDictionary *)dict {
    [self remove21View];
//    [self.dict setValue:kOnClick forKey:@"clickStatus"];
//    KYLog(@"插屏广告跳转字典：%@",dict);
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickView:)]) {
        [self.delegate clickView:self.dict[@"data"]];
    }
}

#pragma mark - Lazy load
- (NSMutableDictionary *)dict {
    if (!_dict) {
        _dict = [[NSMutableDictionary alloc] init];
    }
    return _dict;
}
@end
